 //  ------------ 测试产生虚拟数据 -------------------
 const _ItemBomList = [];
 /*for( var i = 0; i < 20; i++){
     _ItemBomList.push(
         Mock.mock({
             "bom": Mock.mock('@string("upper", 4, 6)'),
             "description":Mock.mock('@ctitle(3, 5)'),
             "reversion": Mock.mock('@cword("ABCD", 1)'),
             "endDate": Mock.mock('@date')
         })
     );
 }*/

const _groupList = [];
for( var i = 0; i < 20; i++){
	_groupList.push(
		Mock.mock({
			"key": Mock.Random.integer(60, 100),
			"label":Mock.Random.string('upper', 5)
		})
	);
}

const _itemDataList = [];

const _bomDataList = [];

const _materialDataList = [];

//  ------------ 赋值 ------------------------
new Vue({
	el: '#app',
	data: function() {
		return {
			activeName: 'first',
            itemBomSearchDialog:false,
            materialSearchDialog:false,
            ItemBomData: [],
            selectBomDetail:[],
			bomForm:{
				bomType:'',
				id:'',
				version:'',
				name:'',
				statusFk:'',
				curVersion:''
			},
			bomTypeList:[{
                label : "主数据",
                value : "主数据"
			},{
                label : "工单",
                value : "工单"
			},{
                label : "车间作业控制",
                value : "车间作业控制"
			}],
			statusList:[{
                label : "新建",
                value : "新建"
            },{
                label : "可下达",
                value : "可下达"
            },{
                label : "作废",
                value : "作废"
            },{
                label : "冻结",
                value : "冻结"
            },{
                label : "保留",
                value : "保留"
            }],
			componentTypeList:[{
                label : "正常",
                value : "正常"
			},{
                label : "测试",
                value : "测试"
			},{
                label : "虚拟",
                value : "虚拟"
            },{
                label : "联产品",
                value : "联产品"
            },{
                label : "副产品",
                value : "副产品"
            }],
			itemData : _itemDataList,
            materialData:_materialDataList,
			bomComponentForm: {
                sequence:10,
                assembleOperationFk:"",
                bomComponentType:"",
                materialVersion:"",
                materialFK:"",
                disassembleOperationFk:"",
                assyDataTypeFk:"",
                maximumNc:0,
                materialFk:"",
                maximumUsage:0,
                qty:0,
                assembleAsReq :""
			},
			bomComponentDialog: false
		}
	},
	methods: {
		query(){
			var me = this,_form = me.bomForm;
			if( _form.id == "" ){
				this.$alert('请输入物料清单号', '提醒', {
					confirmButtonText: '确定'
				});
				return;
			};
			if( _form.version == "" ){
				this.$alert('请输入物料清单版本', '提醒', {
					confirmButtonText: '确定'
				});
				return;
			};
			var param = {
				serviceName:"exposeBomService",
				serviceMethodName:"bomListByBom",
                plant:"TEST",
				id:_form.id,
                version:_form.version
			};
			function callBack( data ){
				var _form = me.bomForm;
				var bomComList = data.retObj.BomComList;
                var bomList = data.retObj.BomList;
                var bomObj = bomList[0];
                document.getElementById("bomIdHidden").value = bomObj.quid;
                _form.bomType=bomObj.bomType;
                _form.name=bomObj.name;
                _form.statusFk=bomObj.statusFk;
                _form.curVersion=bomObj.curVersion=="Y"?true:false;
				if(bomComList.length>0){
                    me._itemDataList = [];
					for(var i=0;i<bomComList.length;i++){
						_itemDataList.push({
							"id":bomComList[i].id,
                            "sequence": bomComList[i].sequence,
                            "assyItem": bomComList[i].materialFk,
                            "assyOper": bomComList[i].assembleOperationFk,
                            "assyQty":  bomComList[i].qty,
                            "componentType": bomComList[i].bomComponentType
						});
					}
				}
			}
			me.ajaxGetRequest( param,callBack );
		},
        addNewComponent(){
         var bomHiddenId = document.getElementById("bomIdHidden").value;
         if(bomHiddenId===""){
             this.$notify({
                 title: '警告',
                 message: '请先查询!',
                 type: 'warning'
             });
         }else{
             this.bomComponentDialog = true;
             var mtableData = this.$refs.componentMultipleTable.data;
             var seq = 10;
             if(mtableData.length>0){
                 seq = mtableData[mtableData.length-1].sequence+10;
             }
             this.bomComponentForm.sequence = seq;
         }
        },
		deleteBom(){
            var me = this;
			var bomQuid = document.getElementById("bomIdHidden").value;
			if( bomQuid === "" ){
                this.$notify.info({
                    title: '提示',
                    message: '请先执行查询操作!'
                });
			}
            var param = {
                serviceName:"exposeBomService",
                serviceMethodName:"deleteBomAndComp",
                quid:bomQuid
            };
            function callBack( data ){
                me.$notify({
                    title: '消息',
                    message: '删除成功!',
                    type: 'success'
                });
                me.bomForm="";
                me.itemData="";
            };
            me.ajaxGetRequest( param,callBack );
		},
        saveBomData(){
            var me = this,_form = me.bomForm;
            var param = {
                serviceName:"exposeBomService",
                serviceMethodName:"addBomData",
                plant:"TEST",
                id:_form.id,
                adopted:"N",
                bomType: _form.bomType,
                curVersion: _form.curVersion==true?"Y":"N",
                version :_form.version,
                name : _form.name,
                bomTem :"N",
                statusFk :_form.statusFk,
                dateEffect :"2017-10-08 12:00:00",
                dateExpire :"2020-12-31 12:00:00"
            };
            function callBack( data ){
                me.$notify({
                    title: '消息',
                    message: '保存成功!'
                });
            };
            me.ajaxGetRequest( param,callBack );
		},
        saveComponentData(){
            var me = this,_form = me.bomComponentForm;
            var param = {
                serviceName:"exposeBomService",
                serviceMethodName:"addComponent",
				sequence:_form.sequence,
                assembleOperationFk:_form.assembleOperationFk,
				bomFk:document.getElementById("bomIdHidden").value,
				bomComponentType:_form.bomComponentType,
				disassembleOperationFk:_form.disassembleOperationFk,
				assyDataTypeFk:_form.assyDataTypeFk,
				maximumNc: _form.maximumNc,
				materialFk:_form.materialFK,
				maximumUsage: _form.maximumUsage,
				qty:_form.qty,
				assembleAsReq:_form.assembleAsReq==true?"Y":"N",
				enabled:"N",
				useItemDefaults:"N",
				testPart:"N"
            };
            function callBack( data ){
                me.bomComponentDialog = false;
                me.$notify({
                    title: '消息',
                    message: '保存成功!'
                });
            };
            me.ajaxGetRequest( param,callBack );
		},
        materialSearch(){
            var me = this;
            var material = me.bomComponentForm.materialFK;
            var param = {
                serviceName:"materialConfigService",
                serviceMethodName:"getMaterialList",
                id:material,
                quid:"",
                plant:"TEST"
            };
            function callBack( data ){
                me._materialDataList=[];
                var materialList = data.retList;
                if(materialList.length>0){
                    for(var i=0;i<materialList.length;i++){
                        _materialDataList.push({
                            "quid": materialList[i].quid,
                            "id": materialList[i].id,
                            "name":materialList[i].name,
                            "version": materialList[i].version
                        });
                    }
                }
                me.materialSearchDialog = true;
            };
            me.ajaxGetRequest( param,callBack );
        },
		filterMethod(){
			console.log('submit!');
		},
		handleClick(tab, event) {
			console.log(tab, event);
		},
        itemBomSearch(){
			var me = this;
			var id = me.bomForm.id;
            var param = {
                serviceName:"exposeBomService",
                serviceMethodName:"bomListByBom",
                plant:"TEST",
				id:id,
                version:""
            };
            function callBack( data ){
                if( data.retObj.BomList.length >0 ){
                    me.ItemBomData = data.retObj.BomList;
                }else{
                    me.ItemBomData = [];
                }
                me.itemBomSearchDialog = true;
            };
            me.ajaxGetRequest( param,callBack );
		},
        removeComponent(){
            var me = this;
            var selectedRowList = me.selectBomDetail;
            var mtableData = me.$refs.componentMultipleTable.data;
            var parString = "";
            if(selectedRowList.length>0){
                for(var i=0;i<selectedRowList.length;i++){
                    var id = selectedRowList[i].id;
                    if( i==0 ){
                        parString = id;
                    }else{
                        parString = parString + "&" + id ;
                    }
                }
                // ---------- 开始后台处理 --------------------------
                var param = {
                    serviceName:"exposeBomService",
                    serviceMethodName:"deleteComponent",
                    bomComponentIdJson:parString
                };
                function callBack( data ){
                    if(date.retCode==="1"){
                        me.$notify.info({
                            title: '消息',
                            message: '删除成功!',
                            type: 'warning'
                        });
                    }else{
                        me.$notify.error({
                            title: '删除出错',
                            message: data.retMsg
                        });
                    }

                };
                me.ajaxGetRequest( param,callBack );

                // ------------  后台处理完成 -------------------------
            }else{
                this.$notify.info({
                    title: '消息',
                    message: '清选择要移除的组件',
                    type: 'warning'
                });
            }
		},
        clearData(){
			var me = this;
            me.bomForm.version = "";
            me.bomForm.id = "";
            me.bomForm.name = "";
            document.getElementById("bomIdHidden").value = "";
            me.bomComponentForm = "";
            me.itemData = [];
		},
		handleIconClick(ev) {
			console.log(ev);
		},
		checkChange(obj,isChecked,children){

		},
		ajaxPostRequest( params, callBackFun )
		{
		 var me = this, _url = "http://localhost:8082/route";
		  axios({
			method: 'post',
			url: _url,
			params: params
			}).then(function(response){
			  if( response.status == 200 ){
				  callBackFun( response.data );
			  }else{
				  console.log( response );
			  }
		  }).catch(function(err){
				console.log(err);
		  });
	    },
		ajaxGetRequest(  params, callBackFun ){
			  var me = this, _url = "http://localhost:8082/route";
			  axios.get( _url,{
				   params: params
			  }).then(function(response){
				  if( response.status == 200 ){
					  callBackFun( response.data );
				  }else{
					  console.log( response );
				  }
			  }).catch(function(err){
					console.log(err);
			  });
		  },
        selectedRowBom(row, event) {
            var bom = row.bom;
            var version = row.version;
            var id = row.id;
            var name = row.name;
            this.itemBomSearchDialog=false;
            this.bomForm.id = id;
            this.bomForm.version = version;
            this.bomForm.name = name;
        },
        selectedRowMaterial(row, event){
            var bom = row.bom;
            var version = row.version;
            var id = row.id;
            var name = row.name;
            this.materialSearchDialog=false;
            this.bomComponentForm.materialFK = id;
            this.bomComponentForm.materialVersion = version;
        },
        selectBomDetailChange( selection ){
            var me = this;
            me.selectBomDetail = selection;
        },
        editBomComponent(index, row){
            var me = this;
            var id = row.id;
            if(id===""){
                this.$notify.error({
                    title: '错误',
                    message: '要修改的行主键为获取到!'
                });
            }else{
                var param = {
                    serviceName:"exposeBomService",
                    serviceMethodName:"getBomComponent",
                    id:id,
                    bomFk:""
                };
                function callBack( data ){
                    var bomComponentObj = data.retList[0];
                    me.bomComponentForm.sequence = bomComponentObj.sequence;
                    me.bomComponentForm.assembleOperationFk = bomComponentObj.assembleOperationFk;
                    me.bomComponentForm.materialFK = bomComponentObj.materialFk;
                    me.bomComponentForm.materialVersion = bomComponentObj.materialFk;
                    me.bomComponentForm.bomComponentType = bomComponentObj.bomComponentType;
                    me.bomComponentForm.disassembleOperationFk = bomComponentObj.disassembleOperationFk;
                    me.bomComponentForm.qty = bomComponentObj.qty;
                    me.bomComponentForm.assyDataTypeFk = bomComponentObj.assyDataTypeFk;
                    me.bomComponentForm.maximumUsage = bomComponentObj.maximumUsage;
                    me.bomComponentForm.maximumNc = bomComponentObj.maximumNc;
                    me.bomComponentForm.assembleAsReq = bomComponentObj.assembleAsReq==="Y"?true:false;
                    me.bomComponentDialog=true;
                }
                me.ajaxGetRequest( param,callBack );
            }
        }
	}
})